package com.yutian.springbootwebflux.config;

import com.fasterxml.classmate.TypeResolver;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 若不设置该SwaggerConfig，则swagger的curl中缺少base-path，如curl -X GET "http://localhost:8099/user/1"
 */
@Configuration
@EnableSwagger2
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket api(TypeResolver typeResolver) {
//        List<RequestParameter> globalRequestParameters = new ArrayList<>();
//        RequestParameter build = new RequestParameterBuilder().name("abc").description("添加swagger公共请求参数abc").required(true).build();
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.yutian.springbootwebflux.controller"))
//                .paths(PathSelectors.any())
//                .build()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
//                .globalRequestParameters(globalRequestParameters)
//                .securitySchemes(securitySchemes()) //添加token
//                .securityContexts(securityContexts()) //swagger配置页面访问是否需要传token
                //.pathMapping("/webflux") 不需要
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .alternateTypeRules(
                        AlternateTypeRules.newRule(
                                typeResolver.resolve(Collection.class, WildcardType.class),
                                typeResolver.resolve(List.class, WildcardType.class))
                )
                .enableUrlTemplating(true)
                .forCodeGeneration(false);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("webflux测试swagger3.0")
                .description("webflux测试swagger3.0")
                .termsOfServiceUrl("Terms of service")
                .version("1.0.0")
                .build();
    }


}
